package br.ufrj.neg.model;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import br.ufrj.neg.util.PropertyLoader;

public class ServiceLocator {
		
	synchronized static public java.sql.Connection getConnection(){
		
		try{
			PropertyResourceBundle rb = PropertyLoader.getPropertyLoader().getPpt_database();
			
			String banco = rb.getString("database.name");
			String userName = rb.getString("database.user");
			String senha = rb.getString("database.password");
			String driver = rb.getString("database.driver");
			
			Class.forName(driver);
			
			java.sql.Connection conn = DriverManager.getConnection(banco,userName,senha);
			conn.setAutoCommit(false);
			/*if (conexoes==null){
				conexoes=new ArrayList<java.sql.Connection>();
			}
			conexoes.add(conn);
			Iterator it = conexoes.iterator();
			int numConAbertas = 0;
			while(it.hasNext()){
				java.sql.Connection c = (java.sql.Connection)it.next();
				if (!c.isClosed()){numConAbertas++;}
			}
			System.out.println(numConAbertas);*/
			//conexoes.add(conn);
			return conn;
		}		
		catch(Exception e){
			System.out.println("Erro ao tentar se conectar ao banco; "+e);
			return null;
		}
	}
	
}